<?php
/**
 * Created by PhpStorm.
 * User: chenjunjie
 * Date: 2020/8/31
 * Time: 2:33 PM
 */


declare(strict_types = 1);

namespace App\Controller\Api;
use App\Controller\Auth\Controller;
use App\Repositories\ApprovalFlowRepository;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;

class ApprovalFlow extends Controller{


    /**
     * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
     *
     * @Inject
     * @var Filesystem
     */
    private $filesystem;

    /**
     * @var ApprovalFlowRepository
     */
    private $approvalFlowRepository;
    public function __construct(ApprovalFlowRepository $approvalFlowRepository)
    {
        parent::__construct();
        $this->approvalFlowRepository = $approvalFlowRepository;
    }

    public function record(){

        $file = $this->request->file('upload');
        $stream = fopen($file->getRealPath(), 'r+');
        $this->filesystem->writeStream(
            'uploads/'.$file->getClientFilename(),
            $stream
        );
        fclose($stream);
//
//        $all = $this->request->all();
//
//        $record = $this->approvalFlowRepository->getRecord($all);
//
//        return $this->response->json( $this->success($record));
    }

    public function recordCount(){

        $data = $this->request->all();
        if(isset($data['typeList'])){
            $resp = [];
            $total = 0;
            $typeList = json_decode($data['typeList']);
            foreach ($typeList as $type){
                $condition = [
                    'type'=>$type,
                    'companyUserId'=>$data['companyUserId'],
                ];
                $resp[$type] = $count = $this->approvalFlowRepository->getRecordCount($condition);
                $total = $total + $count;
            }
            $resp[1] = $resp[1] + @$resp[5];
            $resp['totalCount'] =$total;

            return $this->success($resp);


        }else{
//        var_dump($data);
            $count = $this->approvalFlowRepository->getRecordCount($data);


            return $this->response->json( $this->success($count));
        }

    }
}